package com.th.system.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.common.constant.AuthConstants;
import com.th.common.utils.StringUtils;
import com.th.system.domain.UsQuaVerify;
import com.th.system.mapper.SysQuaVerfifyMapper;
import com.th.system.service.ISysQuaVerifyService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 资质认证 业务层处理
 *
 * @author lcz
 */
@Service
public class SysQuaVerifyServiceImpl extends ServiceImpl<SysQuaVerfifyMapper, UsQuaVerify>  implements ISysQuaVerifyService {

    private static final int LEGAL_STATUS_MIN=1;
    private static final int LEGAL_STATUS_MAX=3;


    @Autowired
    private SysQuaVerfifyMapper quaVerfifyMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public List<UsQuaVerify> getWaitQuaVerByType(int type, int status, int page, int pageSize){

        Page<UsQuaVerify> pageInfo = new Page<>(page, pageSize);
        QueryWrapper<UsQuaVerify> queryWrapper=new QueryWrapper<>();

        queryWrapper.eq("auth_type",type);
        if (status>=LEGAL_STATUS_MIN && status<=LEGAL_STATUS_MAX){
            queryWrapper.eq("examine_result_status",status);
        }
        quaVerfifyMapper.selectPage(pageInfo,queryWrapper);

        List<UsQuaVerify> list=pageInfo.getRecords();
        return list;
    }

    @Override
    public boolean updateStatusById(Integer id, Integer examineResultStatus, String examineResultMessage) {

        QueryWrapper<UsQuaVerify> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);

        UsQuaVerify quaVerify=new UsQuaVerify();
        quaVerify.setExamineResultMessage(examineResultMessage);
        quaVerify.setExamineResultStatus(examineResultStatus);
        quaVerify.setExamineTime(LocalDateTime.now());

        int num = quaVerfifyMapper.update(quaVerify,queryWrapper);
        if (num <= 0){
            return false;
        }

        // 发送站内消息
        rabbitTemplate.convertAndSend(AuthConstants.AUTH_EXCHANGE, AuthConstants.AUTH_INTERNAL_MESSAGE_KEY, id);

        // 发送邮件
        rabbitTemplate.convertAndSend(AuthConstants.AUTH_EXCHANGE, AuthConstants.AUTH_MAIL_MESSAGE_KEY, id);
        return true;
    }

    @Override
    public boolean deleteQuaVerById(int id) {
        QueryWrapper<UsQuaVerify> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);

        UsQuaVerify usQuaVerify = new UsQuaVerify();
        usQuaVerify.setAuthType(-1);

        int num=quaVerfifyMapper.update(usQuaVerify,queryWrapper);
        if(num>0){
            return true;
        }

        return false;
    }

    @Override
    public boolean idIsExsit(int id) {

        QueryWrapper<UsQuaVerify> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        queryWrapper.ne("auth_type",-1);

        UsQuaVerify usQuaVerify= quaVerfifyMapper.selectOne(queryWrapper);
        if (usQuaVerify==null){
            return false;
        }
        return true;

    }

}
